package com.wan.commonsdk;

import java.util.HashMap;
import java.util.Map;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class WanDBHelper extends SQLiteOpenHelper{
	private final static String DATABASE_NAME = "wan_db";
	private final static int DATABASE_VERSION = 1;
	private final static String TABLE_NAME = "active_info";
	public final static String FIELD_ADCICEID = "advice_id";
	public final static String FIELD_STATUS = "status";

	public WanDBHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		String sql = "Create table " + TABLE_NAME + "(" + FIELD_ADCICEID
				+ " text," + FIELD_STATUS + " integer);";
		db.execSQL(sql);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		String sql = "DROP TABLE IF EXISTS " + TABLE_NAME;
		db.execSQL(sql);
		onCreate(db);
	}
	
	public Map<String, Object> select() {
		Map<String, Object> result = new HashMap<String, Object>();
		String advice_id = "";
		int status = 0;
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db
				.query(TABLE_NAME, null, null, null, null, null, null);
		if (cursor.moveToFirst()) {
			int nameColumnIndex = cursor.getColumnIndex(FIELD_ADCICEID);
			int statusColumnIndex = cursor.getColumnIndex(FIELD_STATUS);
			if (nameColumnIndex != -1) {
				advice_id = cursor.getString(nameColumnIndex);

			}
			if (statusColumnIndex != -1) {
				status = cursor.getInt(statusColumnIndex);
			}
		}
		result.put("advice_id", advice_id);
		result.put("status", status);
		cursor.close();
		cursor = null;
		return result;
	}
	
	public long insert(String adciveid, int status) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues cv = new ContentValues();
		cv.put(FIELD_ADCICEID, adciveid);
		cv.put(FIELD_STATUS, status);
		long row = db.insert(TABLE_NAME, null, cv);
		return row;
	}

	

}
